import { AddRoleModel, RoleModel } from '@/api/system/role/RoleModel'
import useInstance from '@/hooks/useInstance'
import { EditType, Title } from '@/types/BaseEnum'
import { DialogModel } from '@/types/BaseType'
import { ElForm } from 'element-plus'
import { ref } from 'vue'

export default function useRoleAddAndEdit(
    dialogModel: DialogModel,
    onShow: any,
    onClose: any,
    addModel: RoleModel,
    emit: any
) {
    const { global } = useInstance()
    //表单ref属性
    const addRoleForm = ref<InstanceType<typeof ElForm>>()

    const onConfirm = () => {
        addRoleForm.value?.validate((valid) => {
            if (valid) {
                emit('save', addModel)
                onClose()
            }
        })
    }

    // 给父组件调用，显示弹框
    const show = async (type: string, row?: AddRoleModel) => {
        console.log('show：', row)
        // 设置弹框的标题
        type == EditType.ADD
            ? (dialogModel.title = Title.ADD)
            : (dialogModel.title = Title.EDIT)

        // 清空表单
        global.$resetForm(addRoleForm.value, addModel)
        // 把要编辑的数据，放到表单绑定的model里面
        // if (EditType.EDIT === type) {
        //     global.$objCoppy(row, addModel)
        // }
        global.$objCoppy(row, addModel)

        // 设置type
        addModel.type = type
        // 显示弹框
        onShow()
    }

    return {
        onConfirm,
        show,
        addRoleForm
    }
}
